package com.android.accessibility.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.android.accessibility.R;

public class InputDialog extends Dialog implements android.view.View.OnClickListener {

    private TextView mTextView;
    private EditText mEditView;
    private String mTitle;

    public InputDialog(Context context) {
        super(context);
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public void setTitle(int resId) {
        mTitle = getContext().getResources().getString(resId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.input_dialog);
        mTextView = (TextView) findViewById(R.id.title);
        mEditView = (EditText) findViewById(R.id.edittext);
        if (!TextUtils.isEmpty(mTitle)) {
            mTextView.setText(mTitle);
        }
        View view = null;
        view = findViewById(R.id.ok);
        view.setOnClickListener(this);
        view = findViewById(R.id.cancel);
        view.setOnClickListener(this);
        updateWindow();
    }

    private void updateWindow() {
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = (int) (0.8f * dm.widthPixels);
        getWindow().setAttributes(p);
    }

    private OnInputListener mOnInputListener;

    public void setOnInputListener(OnInputListener l) {
        mOnInputListener = l;
    }

    public interface OnInputListener {
        public void onInput(String text);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ok) {
            if (mOnInputListener != null) {
                mOnInputListener.onInput(mEditView.getText().toString());
            }
        }
        dismiss();
    }
}
